<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Systemtemplet_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
 	function arrays($key){
    	$data = array(
    		'sys_templet_type' 			=> array("1"=>"邮件模板", "2"=>"短信模板","3"=>"站内信模板"),
    		'sys_templet_user_send' 			=> array("1"=>"邮件", "2"=>"短信","3"=>"站内信"),
    		'sys_templet_sub' 			=> array("1"=>"msgemail", "2"=>"msgsms","3"=>"msgzhs_letter"),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    
    function templet_list($data){
    	//封装查询内容
		$select = "id,title,application_scene,is_stop,type,content,add_time";
		
		//封装条件
		$_where = " where 1=1 ";
		
    	if(isset($data['where']['type']) && $data['where']['type'] != ''){
			$_where .= " and type=".$data['where']['type'];
		}
		
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['txt']) && $data['where']['txt'] != ''){
			$_where .= " and (title like '%".$data['where']['txt']."%' or content like '%".$data['where']['txt']."%')";
		}
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_system_templet";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['templet_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_system_templet";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['templet_count'] = $query->row()->count;
		
		return $data;
    }
    
    
	function get_templet_obj($id){
		$mkey = "templet_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,application_scene,is_stop,type,content";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_system_templet');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
	function get_templet_title($title){
		$mkey = "templet_obj_".$title;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,application_scene,is_stop,type,content";
		$this->db->select($select);
		$this->db->where(array('title'=>$title));
		$query = $this->db->get('rsh_system_templet');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
    
	
	function add($data = array()){
		$this->db->insert('rsh_system_templet', $data);
		return $this->db->insert_id();
	}
	

	function update($id, $data = array()){
		$this->mycache->delete('templet_obj_'.$id);
		$this->db->where('id', $id);
		$this->db->update('rsh_system_templet', $data);
		return $this->db->affected_rows();
	}
	
	
	function get_templet_list($type){
		$key = 'templet_list_'.$type;
		$list = $this->mycache->get_array($key);
		if(!$list){
			$this->db->select('id,title');	
			$this->db->where(array('is_stop'=>0,'type'=>$type));		
			$query = $this->db->get('rsh_system_templet');		
			$result = $query->result_array();
			$list = array();
			if($result){
				foreach($result as $v){
					$list[$v['id']] = $v['title'];
				}
				$this->mycache->set_array($key,$list);
			}
		}
		return $list;
	}
	
	
	function user_send_add($data = array()){
		$this->db->insert('rsh_user_send', $data);
		return $this->db->insert_id();
	}
	
	
	function user_send_update($id, $data = array()){
		$this->mycache->delete('send_obj_'.$id);
		$this->db->where('id', $id);
		$this->db->update('rsh_user_send', $data);
		return $this->db->affected_rows();
	}
	
	
	function user_send_obj($id, $data = array()){
		$mkey = "send_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,type,content,status,add_time";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_user_send');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
	
	function user_send_list($data){
    	//封装查询内容
		$select = "id,title,type,content,status,add_time";
		
		//封装条件
		$_where = " where 1=1 ";
		
		if(isset($data['where']['status']) && $data['where']['status'] != ''){
			$_where .= " and status=".$data['where']['status'];
		}
		
    	if(isset($data['where']['type']) && $data['where']['type'] != ''){
			$_where .= " and type=".$data['where']['type'];
		}
		
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['txt']) && $data['where']['txt'] != ''){
			$_where .= " and (title like '%".$data['where']['txt']."%' or content like '%".$data['where']['txt']."%')";
		}
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_user_send";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['user_send_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_user_send";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['user_send_count'] = $query->row()->count;
		
		return $data;
    }
    
	
	
    	
}